package com.after.saas.log.event;


import com.after.saas.common.constants.Constants;
import com.after.saas.common.constants.JwtEnum;
import com.after.saas.common.constants.Result;
import com.after.saas.common.model.dto.OperatorLogDTO;
import com.after.saas.common.utils.RequestUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * 注解形式的监听 异步监听日志事件
 *
 * @Classname OperateLogListener
 * @Description TODO
 * @Date 2021/6/11 15:21
 * @Created by hjchen
 */

@Slf4j
@Component
public class OperateLogListener {

//    @Autowired
//    private LogClient logClient;

    @Autowired
    private RestTemplate restTemplate;

    @Async
    @Order
    @EventListener(OperateLogEvent.class)
    public void saveSysLog(OperateLogEvent event) {
        OperatorLogDTO operateLog = (OperatorLogDTO) event.getSource();
        //保存日志
        HttpEntity requestEntity = new HttpEntity(operateLog, RequestUtil.buildHeader());
        try {
            ResponseEntity responseEntity = RequestUtil.postUtil(restTemplate, Constants.RpcRequestAddress.operateLogSave, requestEntity);
//            log.warn("ResponseEntity {}", responseEntity.getBody());
//            Result resultHttpEntity = restTemplate.exchange(Constants.RpcRequestAddress.operateLogSave, HttpMethod.POST, requestEntity, Result.class).getBody();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("日志记录失败！{}", e.getMessage());
        }
//        logClient.saveLog(operateLog);
    }
}
